
//userA
let userA = null;

//userB
let userB = null;

$(function (){
   listen_query_btn();
   listen_query_btn2();

    listen_add_money_btn();
    listen_confirm_add_money_btn();
    listen_sub_money_btn();
    listen_confirm_sub_money_btn();

    transfer();
});

//**********************************查询UserA生成表格*********************************************
//监听查询User按钮
function listen_query_btn()
{
    $('#query-user-btn').click(function (){
        let userName = $('#name').val();
        let userPassword = $('#password').val();
        queryUser(userName,userPassword);
    });
}

//通过name,password查询User
function  queryUser(userName,userPassword)
{
    $.ajax({
        url: "/user/login/"+ userName + "/" + userPassword,
        type: "GET",
        success: function (result){
            if(result.extend.login_user !== null)
            {
                alert("查询成功!");
                userA = result.extend.login_user;
                create_table();
            }
            else
            {
                alert("用户名或密码错误!");
                userA = null;
            }
        }
    });
}
//查询User动态生成结果
function create_table()
{
    //清空表格
    $("#user-table tbody").empty();

    let item = userA;
    let userNumberTd = $("<td></td>").append(item.number);
    let userNameTd = $("<td></td>").append(item.name);
    let userGenderTd = $("<td></td>").append(item.gender === "male" ? "男" : "女");
    let userDateTd = $("<td></td>").append(item.date);
    let userMoneyTd = $("<td></td>").append(item.money);

    let depositBtn = $("<button></button>").addClass("btn btn-primary btn-sm deposit-btn")
        .append(" ")
        .append($("<span></span>").addClass("glyphicon glyphicon-euro"))
        .append("存款");

    let withdrawalBtn = $("<button></button>").addClass("btn btn-danger btn-sm withdrawal-btn")
        .append(" ")
        .append($("<span></span>").addClass("glyphicon glyphicon-log-in"))
        .append("取款");


    let btnTd = $("<td></td>").append(depositBtn)
        .append(" ")
        .append(withdrawalBtn);

    $("<tr></tr>")
        .append(userNumberTd)
        .append(userNameTd)
        .append(userGenderTd)
        .append(userDateTd)
        .append(userMoneyTd)
        .append(btnTd)
        .appendTo("#user-table tbody");
}


//**********************************查询UserB*****************************************
function listen_query_btn2()
{
    $('#query-user-btn2').click(function (){
        let userName = $('#name2').val();
        let userPassword = $('#password2').val();
        queryUser2(userName,userPassword);

    });
}


//通过name,password查询User
function  queryUser2(userName,userPassword)
{
    $.ajax({
        url: "/user/login/"+ userName + "/" + userPassword,
        type: "GET",
        success: function (result){
            if(result.extend.login_user !== null)
            {
                alert("查询成功!");
                userB = result.extend.login_user;
                create_table2();
            }
            else
            {
                alert("用户名或密码错误!");
                userB = null;
            }
        }
    });
}
function create_table2()
{
    //清空表格
    $("#user-table2 tbody").empty();

    if(userB !== null)
    {
        if(userB.name === userA.name)
        {
            alert("重复查询！");
            userB = null;
            $('#name2').val("");
            $('#password2').val("");
            return false;

        }
    }

    let item = userB;
    let userNumberTd = $("<td></td>").append(item.number);
    let userNameTd = $("<td></td>").append(item.name);
    let userGenderTd = $("<td></td>").append(item.gender === "male" ? "男" : "女");
    let userDateTd = $("<td></td>").append(item.date);
    let userMoneyTd = $("<td></td>").append(item.money);

    let depositBtn = $("<button></button>").addClass("btn btn-primary btn-sm deposit-btn2")
        .append(" ")
        .append($("<span></span>").addClass("glyphicon glyphicon-euro"))
        .append("存款");

    let withdrawalBtn = $("<button></button>").addClass("btn btn-danger btn-sm withdrawal-btn2")
        .append(" ")
        .append($("<span></span>").addClass("glyphicon glyphicon-log-in"))
        .append("取款");


    let btnTd = $("<td></td>").append(depositBtn)
        .append(" ")
        .append(withdrawalBtn);

    $("<tr></tr>")
        .append(userNumberTd)
        .append(userNameTd)
        .append(userGenderTd)
        .append(userDateTd)
        .append(userMoneyTd)
        .append(btnTd)
        .appendTo("#user-table2 tbody");
}

//**********************************userA存取款********************************************

//监听存款按钮
function listen_add_money_btn()
{
    $(document).on("click",".deposit-btn",function (){
        //回显name,money
        $('#name-static').text(userA.name);
        $('#money').val(userA.money);
        $('#add-money').val(0);

        $("#deposit-modal").modal({
            backdrop:"static"
        });
    });
}

//监听确认存款按钮
function listen_confirm_add_money_btn()
{
    $('#confirm-add-money').click(function (){
        //1. 进行相关数据校验
        let user = userA;
        user.money = Number(user.money) + Number($('#add-money').val());

        //2. 发送ajax请求保存修改后的User
        $.ajax({
            url: "/user/save/" + userA.number,
            type: "PUT",
            data: user,
            success: function () {
                //1.关闭模态框
                $('#deposit-modal').modal('hide');
                //2.显示查询后的结果
                alert("存款成功!");
                queryUser(userA.name,userA.password);
            }
        });
    });
}

//监听取款按钮
function listen_sub_money_btn()
{
    $(document).on("click",".withdrawal-btn",function (){
        //回显name,money
        $('#name-static-withdrawal').text(userA.name);
        $('#money-withdrawal').val(userA.money);
        $('#sub-money').val(0);

        $("#withdrawal-modal").modal({
            backdrop:"static"
        });
    });
}

//监听确认取款按钮
function listen_confirm_sub_money_btn()
{
    $('#confirm-sub-money').click(function (){
        //1. 进行相关数据校验
        let user = userA;
        user.money = Number(user.money) - Number($('#sub-money').val());
        if(Number(user.money)<0)
        {
            alert("取出金额超支！");
            return false;
        }
        //2. 发送ajax请求保存修改后的User
        $.ajax({
            url: "/user/save/" + userA.number,
            type: "PUT",
            data: user,
            success: function () {
                //1.关闭模态框
                $('#withdrawal-modal').modal('hide');
                //2.显示查询后的结果
                alert("取款成功!");
                queryUser(userA.name,userA.password);
            }
        });
    });
}

//*******************************转账业务*********************************************
//转账业务
function transfer()
{
    listen_transfer_btn();
    listen_confirm_transfer_btn();
}

//监听转账按钮
function listen_transfer_btn()
{
    $('#transfer').click(function(){
        if(userA === null || userB === null)
        {
            alert("还没选择转出转入账户！");
            return false;
        }
        //弹出转账模态框
        //回显信息
        $('#transfer-name').text(userA.name);
        $('#transfer-money').val(userA.money);
        $('#transfer-sub-money').val(0);
        $('#transfer-name2').text(userB.name);

        $("#transfer-modal").modal({
            backdrop:"static"
        });

    });
}

//监听确认转账按钮
function listen_confirm_transfer_btn()
{
    $('#confirm-transfer-money').click(function (){
        //1. 进行相关数据校验
        userA.money = Number(userA.money) - Number($('#transfer-sub-money').val());
        if(Number(userA.money) < 0)
        {
            alert("取出金额超支！");
            return false;
        }
        userB.money = Number(userB.money) + Number($('#transfer-sub-money').val());

        let array = [];
        array.push(userA);
        array.push(userB);
        let jsons = JSON.stringify(array);

        console.log(jsons);
        //2. 发送ajax请求,userA扣钱,userB加钱
        $.ajax({
            url: "/user/transfer",
            type: "PUT",
            dataType: 'JSON',
            data: {"jsons": jsons},
            success: function () {
                //1.关闭模态框
                $('#transfer-modal').modal('hide');
                //2.显示查询后的结果
                alert("转账成功!");
                queryUser(userA.name,userA.password);
                queryUser2(userB.name,userB.password);
            }
        });

    });
}